package com.cn.medicalcare.controller.frontservlet;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Message;
import com.cn.medicalcare.pojo.News;
import com.cn.medicalcare.service.MessageService;
import com.cn.medicalcare.service.serviceimpl.MessageServiceImpl;
import com.cn.medicalcare.service.serviceimpl.NewsServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/messageinsert.json")
public class MessageInsertServlet extends HttpServlet {

    private MessageService messageService = MessageServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String message = req.getParameter("message");

        Message msg = new Message();
        Map<String, Object> map = new LinkedHashMap<>();
        try {
            if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(email) && StringUtils.isNotEmpty(message)) {
                msg.setName(name).setEmail(email).setMessage(message);
                if (messageService.insertMessage(msg)) {
                    map.put("status", 200);
                } else {
                    map.put("status", 500);
                }
            } else {
                map.put("status", 400);
            }
        } catch (Exception e) {
            map.put("status", 500);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
